//
//  JSQCategoryView.m
//  SkyHospital
//
//  Created by C_HAO on 15/4/22.
//  Copyright (c) 2015年 GrayWang. All rights reserved.
//

#import "JSQCategoryView.h"
#import "JSQCategoryCollectionViewCell.h"
#import "UIImage+JSQMessages.h"


@implementation JSQCategoryView


- (void)awakeFromNib {
    [super awakeFromNib];

    [_collectionView registerNib:[UINib nibWithNibName:@"JSQCategoryCollectionViewCell" bundle:[NSBundle bundleForClass:[JSQCategoryCollectionViewCell class]]] forCellWithReuseIdentifier:@"JSQCategoryCollectionViewCell"];
}

//定义展示的UICollectionViewCell的个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [self.delegate categoryView:collectionView numberOfItemsInSection:section];
}

//定义展示的Section的个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

//每个UICollectionView展示的内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JSQCategoryCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"JSQCategoryCollectionViewCell" forIndexPath:indexPath];

    if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {

    } else {
        cell.categoryLabel.hidden = YES;
    }


    [self.delegate categoryView:collectionView cellForItemAtIndexPath:indexPath categoryImageView:cell.categoryImageView categoryLabel:cell.categoryLabel];


    return cell;
}

#pragma mark --UICollectionViewDelegateFlowLayout

//定义每个Item 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    float width = (CGRectGetWidth([UIScreen mainScreen].bounds))/4;
    return CGSizeMake(floor(width), floor(width));
}

//定义每个UICollectionView 的 margin
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    return UIEdgeInsetsZero;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0.0f;
}

#pragma mark --UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    [self.delegate selectedCategoryType:indexPath.row];
}

- (void)dealloc {
    _delegate = nil;
}

@end
